<?php
namespace Manage\Controller;

/**
 * 权限
 * Class RbacController
 * @package Manage\Controller
 */
class RbacController extends BaseController {

    /**
     * 角色
     */
    public function role(){
        $m = M('b2c_manage_role');
        $p = I('p',1,'intval');
        $role = $m->page($p.',20')->select();
        $count      = $m->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->_arr['role'] = $role;
        $this->_showDisplay();
    }

    /**
     * 增加角色
     */
    public function roleAdd(){
        $name = I('name', '', htmlspecialchars());
        if (!empty($name)) {
            $m = M('b2c_manage_role');
            $m->add(['name' => $name]);
            $this->success('新增成功', U('manage/rbac/role'));
            exit;
        }
        $this->_showDisplay();
    }

    /**
     * 编辑角色
     */
    public function roleEdit(){
        $id = I('id', '', intval());
        $m = M('b2c_manage_role');
        $role = $m->where(['id' => $id])->find();
        if ($_POST) {
            if ($role) {
                $name = I('name', '', htmlspecialchars());
                $m->where(['id' => $id])->save(['name' => $name]);
                $this->success('修改成功', U('manage/rbac/role'));
                exit;
            }
        }
        $this->_arr['role'] = $role;
        $this->_showDisplay();
    }

    public function roleDel(){
        $id = I('id', '', intval());
        M('sdb_b2c_manage_role')->delete($id);
        $this->success('删除成功', U('manage/rbac/role'));
    }

    /**
     * 门店用户管理
     */
    public function user(){

        $m = M('pam_account');

        $sql = "select a.*,c.name from sdb_pam_account a 
left join sdb_manage_user_role b 
on a.account_id = b.manege_id 
left join sdb_b2c_manage_role c 
on b.role_id = c.id ";
        $user = $m->query($sql);

        $this->assign('user',$user);
        $this->display();
    }

    public function userAdd(){
        if ($_POST){
            $name = I('name', '', htmlspecialchars());
            $password = $_POST['password'];
            if (empty($name) || empty($password)) {
                $this->error('请填写用户名或者密码');
            }else {
                $password = md5($_POST['password']);
                $m = M('pam_account');
                $m->add(['login_name' => $name, 'login_password' => $password]);
                $this->success('新增成功', '/manage/rbac/user');
                exit;
            }
        }
        $this->display();
    }

    public function userEdit(){
        $id = I('id', '', intval()); // 用户id
        $m = M('pam_account');
        $user_role_model = M('manage_user_role');
        $user_role_info = $user_role_model->where(['manege_id'=>$id])->find(); // 查询用户角色

        $roleOne = '';
        if ($user_role_info) {
            $roleOne = M("b2c_manage_role")->where(['id'=>$user_role_info['role_id']])->find();
            $this->assign('roleOne',$roleOne);
        }
        $user = $m->where(['account_id' => $id])->find();
        if ($_POST) {
            if ($user) {
                $name = I('name', '', htmlspecialchars());
                if (empty($name)) {
                    $this->error('请填写用户名');
                }
                $data['login_name'] = $name;
                $password = $_POST['password'];
                if (!empty($password)) {
                    $data['login_password'] = md5($password);
                }
                $m->where(['account_id' => $id])->save($data);

                $role_id = I('role_id', '', intval());
                $user_role_data = ['role_id' => $role_id, 'manege_id' => $id];
                $user_role_model->where(['manege_id' => $id])->delete();
                $user_role_model->add($user_role_data);

                $this->success('修改成功', U('manage/rbac/user'));
                exit;
            }
        }
        $role = M('b2c_manage_role')->select();

        $this->assign('user',$user);
        $this->assign('role',$role);
        $this->display();
    }

    public function userDel(){
        $id = I('id', '', intval());
        M('pam_account')->delete($id);
        M('webpos_local_store_login')->delete($id);
        $this->success('删除成功', U('manage/rbac/user'));
    }

    /**
     * 菜单
     */
    public function menu(){
        $m = M('b2c_manage_menu');
        $p = I('p',1,'intval');
        $menu = $m->page($p.',20')->select();
        $count      = $m->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('menu',$menu);
        $this->display();
    }

    public function menuAdd(){
        $nav_menu = M('b2c_manage_nav_menu')->select();
        if ($_POST){
            $name = I('name', '', htmlspecialchars());
            $value = I('value', '', htmlspecialchars());
            $group = I('group', '', htmlspecialchars());
            $controller = I('controller', '', htmlspecialchars());
            $method = I('method', '', htmlspecialchars());
            $nav_menu_id = I('nav_menu', '', htmlspecialchars());
            $data = [
                'name' => $name,
                'value' => $value,
                'group' => ucfirst($group),
                'controller' => ucfirst($controller),
                'method' => $method,
                'nav_menu_id' => $nav_menu_id
            ];
            $m = M('b2c_manage_menu');
            $m->add($data);
            $this->success('新增成功', U('manage/rbac/menu'));
            exit;

        }
        $this->assign(['nav_menu' => $nav_menu]);
        $this->display();
    }
    public function menuEdit(){

        $id = I('id', '', intval());
        $m = M('b2c_manage_menu');
        $menu = $m->where(['id' => $id])->find();
        if ($_POST) {
            if ($menu) {
                $name = I('name', '', htmlspecialchars());
                $value = I('value', '', htmlspecialchars());
                $group = I('group', '', htmlspecialchars());
                $controller = I('controller', '', htmlspecialchars());
                $method = I('method', '', htmlspecialchars());
                $nav_menu_id = I('nav_menu', '', htmlspecialchars());
                $data = [
                    'name' => $name,
                    'value' => $value,
                    'group' => ucfirst($group),
                    'controller' => ucfirst($controller),
                    'method' => $method,
                    'nav_menu_id' => $nav_menu_id
                ];
                $m->where(['id' => $id])->save($data);
                $this->success('修改成功', U('manage/rbac/menu'));
                exit;
            }
        }
        $nav_menu = M('b2c_manage_nav_menu')->select();

        $this->assign('menu',$menu);
        $this->assign('nav_menu',$nav_menu);
        $this->display();
    }
    public function menuDel(){
        $id = I('id', '', intval());
        M('b2c_manage_menu')->delete($id);
        $this->success('删除成功', U('manage/rbac/menu'));
    }

    /**
     * 添加权限
     */
    public function roleAddMenu(){
        $id = I('id', '', intval()); // 角色id


        $nav_menu = M('b2c_manage_nav_menu')->select();
        $menu = M('b2c_manage_menu')->select();
        $role_menu = M('b2c_manage_role_menu')->where(['role_id' => $id])->select();

        if ($_POST) {
            $m = M('b2c_manage_role_menu');
            $menu_ids = I('name', '', htmlspecialchars());
            $m->where(['role_id' => $id])->delete();
            foreach ($menu_ids as $v) {
                $data = [ 'role_id' => $id,'menu_id'=>$v];
                $m->add($data);
            }
            $this->success('成功');
            exit;
        }

        $this->assign(['menu'=> $menu, 'id'=>$id, 'nav_menu' => $nav_menu, 'role_menu' => $role_menu]);

        $this->display();
    }
}